package com.xb.loan.admin.web.controller;

import com.xb.loan.admin.bean.dict.District;
import com.xb.loan.admin.service.dict.DistrictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/district")
public class DistrictController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(DistrictController.class);
    @Autowired
    DistrictService districtService;

    @ResponseBody
    @RequestMapping(value = "queryDistrict", method = RequestMethod.POST)
    public Map<String, Object> queryDistrict(String parentId, HttpServletRequest req) {
        logger.info("DistrictController.queryDistrict parentId=" + parentId);
        Map<String, Object> map = new HashMap<String, Object>();
        String msg = "操作成功";
        int errorCode = 0;
        if (parentId != null && parentId.length() > 0 ) {
            try {
                List<District> list =  districtService.queryDistrictByParentDistrictId(parentId);
                map.put("districtList",list);
            } catch (Exception e) {
                msg = e.getMessage();
                errorCode = 1;
                logger.error(msg, e);
            }
        } else {
            msg = "查询地区报错";
            errorCode = 1;
        }
        map.put("msg", msg);
        map.put("errorCode", errorCode);
        return map;
    }
}
